package com.xfc.quartz.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xfc.message.NoticeMessage;
import com.xfc.school.domain.ScBuyCourse;
import com.xfc.school.domain.ScCourse;
import com.xfc.school.domain.ScStudent;
import com.xfc.school.service.IScBuyCourseService;
import com.xfc.school.service.IScCourseService;
import com.xfc.school.service.IScStudentService;
import com.xfc.wx.domain.ScWxUser;
import com.xfc.wx.service.IScWxUserService;
import jdk.nashorn.internal.ir.debug.PrintVisitor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * 课程进度
 *
 * @author lcj
 */
@Slf4j
@Component("ProgressTask")
public class ProgressTask {

    @Autowired
    private NoticeMessage message;

    @Autowired
    private IScBuyCourseService buyCourseService;

    @Autowired
    private IScStudentService studentService;

    @Autowired
    private IScWxUserService wxUserService;

    @Autowired
    private IScCourseService courseService;


    public void progress(){
        //查询课程购买进度
        QueryWrapper<ScBuyCourse> queryWrapper=new QueryWrapper<>();
        queryWrapper.lt("class_surplus_time",5);
        List<ScBuyCourse> courseList=buyCourseService.list(queryWrapper);
        //发送通知
        courseList.forEach(l->{
            //查询课程信息
            ScCourse course=courseService.getById(l.getClassId());
            //查询学生信息
            ScStudent student=studentService.getById(l.getStudentId());
            if (Objects.nonNull(student)){
                //查询openid
                ScWxUser wxUser=wxUserService.getOne(new QueryWrapper<ScWxUser>().eq("mobile",student.getStudentMobile()));
                if (Objects.nonNull(wxUser)){
                    //微信通知
                    message.courseProgress(wxUser.getOpenId(),student.getParentName(),Objects.isNull(course.getClassName())?"无":course.getClassName(),"正常",l.getClassSurplusTime(),l.getClassTime(),l.getClassTime()-l.getClassSurplusTime());
                }else {
                    log.error("用户信息为空,获取openid为空,手机号:{}",student.getStudentMobile());
                }
            }else {
                log.error("学生信息为空,学生id:{}",l.getStudentId());
            }
        });

    }
}
